package com.snuproject.lgt.imgtrans;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.lge.real3d.Real3D;
import com.lge.real3d.Real3DInfo;
import com.snuproject.lgt.R;

public class S3DSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private Point mWindowPos;

    private Real3D mReal3D;
    private Real3DInfo ss_info= new Real3DInfo(true, Real3D.REAL3D_TYPE_SS, 0);
    private boolean mIsS3D = true;

    private int mLeftResourceID;
    private int mRightResourceID;

    // canvas size
    private int mCanvasWidth;
    private int mCanvasHeight;

    // image information
    private int mImgWidth;
    private int mImgHeight;

    private Rect mImgRect;
    private Rect mCanvasRect, mLeftCanvasRect, mRightCanvasRect;
    private Rect mPersonRect, mMoonRect;

    private Bitmap mLeftImage;
    private Bitmap mRightImage;
    
    private boolean bMoon;
    private Bitmap mMoon;

    public S3DSurfaceView(Context context, int leftImgID, int rightImgID) {
        super(context);

        mLeftResourceID = leftImgID;
        mRightResourceID = rightImgID;
        mReal3D = new Real3D(getHolder());
        mReal3D.setReal3DInfo(ss_info);
        getHolder().addCallback(this);
        bMoon = true;
    }

    public S3DSurfaceView(Context context) {
        super(context);

        int index = GlobalValues.getInstance().getBGIndex();
        mLeftResourceID = GlobalValues.getInstance().getBGImgID()[index][0];
        mRightResourceID = GlobalValues.getInstance().getBGImgID()[index][1];
        mReal3D = new Real3D(getHolder());
        mReal3D.setReal3DInfo(ss_info);
        getHolder().addCallback(this);
        bMoon = false;
    }


    public void draw() {
        Canvas c;
        c = getHolder().lockCanvas(null);
        if (c == null)
            return;
        drawSurfaceCanvas(c);
        getHolder().unlockCanvasAndPost(c);
    }

    public void drawSurfaceCanvas(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        if (mIsS3D) {
            canvas.drawBitmap(mLeftImage, mImgRect, mLeftCanvasRect, null);
            canvas.drawBitmap(mRightImage, mImgRect, mRightCanvasRect, null);
            
            Bitmap person = GlobalValues.getInstance().getPerson();
            if( person != null ) {
            	Point off = GlobalValues.getInstance().getPersonOff();
            	mPersonRect.offset(-5 + off.x, off.y);
                canvas.drawBitmap(person, mPersonRect, mLeftCanvasRect, null);
            	mPersonRect.offset(5 + off.x, off.y);
                canvas.drawBitmap(person, mPersonRect, mRightCanvasRect, null);
            }
            if( bMoon ) {
            	mMoonRect.offset(-5, 0);
                canvas.drawBitmap(mMoon, mMoonRect, mLeftCanvasRect, null);
            	mMoonRect.offset(5, 0);
                canvas.drawBitmap(mMoon, mMoonRect, mRightCanvasRect, null);
            }
        } else {
            canvas.drawBitmap(mLeftImage, mImgRect, mCanvasRect, null);
        }
    }

//    int preX, preY;
//    Point down1, down2;
//    Point move1, move2;

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int action = event.getAction() & MotionEvent.ACTION_MASK;
//        int x = (int) event.getX();
//        int y = (int) event.getY();
//
//        switch (action) {
//        case MotionEvent.ACTION_POINTER_DOWN:
//            if (event.getPointerCount() > 1) {
//                System.out.println("multi-touch down!!");
//                down1 = new Point((int) event.getX(0), (int) event.getY(0));
//                down2 = new Point((int) event.getX(1), (int) event.getY(1));
//            }
//            break;
//        case MotionEvent.ACTION_MOVE:
//            if (event.getPointerCount() == 1) {
//                // single touch : move
//                move(preX - x, preY - y);
//                preX = x;
//                preY = y;
//            } else {
//                // multi touch : zoom
//                move1 = new Point((int) event.getX(0), (int) event.getY(0));
//                move2 = new Point((int) event.getX(1), (int) event.getY(1));
//                zoom(down1, down2, move1, move2);
//                down1 = move1;
//                down2 = move2;
//            }
//            break;
//        case MotionEvent.ACTION_UP:
//            break;
//        case MotionEvent.ACTION_DOWN:
//            preX = x;
//            preY = y;
//            break;
//        }
//        draw();
//        return true;
//    }

//    private void move(int offsetX, int offsetY) {
//        float xUnit, yUnit;
//        int moveX, moveY;
//        xUnit = (float) mImgWidth / (float) mCanvasWidth;
//        yUnit = (float) mImgHeight / (float) mCanvasHeight;
//
//        moveX = (int) ((float) offsetX * xUnit);
//        moveY = (int) ((float) offsetY * yUnit);
//
//        // X
//        if (moveX < 0) {
//            if ((mImgRect.left + moveX) >= 0)
//                mImgRect.offset(moveX, 0);
//        } else {
//            if ((mImgRect.right + moveX) < mImgWidth)
//                mImgRect.offset(moveX, 0);
//        }
//        // Y
//        if (moveY < 0) {
//            if ((mImgRect.top + moveY) >= 0)
//                mImgRect.offset(0, moveY);
//        } else {
//            if ((mImgRect.bottom + moveY) < mImgHeight)
//                mImgRect.offset(0, moveY);
//        }
//    }
//
//    private Rect makeRect(Point p1, Point p2) {
//        int left, right, top, bottom;
//        if (p1.x < p2.x) {
//            left = p1.x;
//            right = p2.x;
//        } else {
//            left = p2.x;
//            right = p1.x;
//        }
//        if (p1.y < p2.y) {
//            top = p1.y;
//            bottom = p2.y;
//        } else {
//            top = p2.y;
//            bottom = p1.y;
//        }
//        return new Rect(left, top, right, bottom);
//    }
//
//    void growRectInX(Rect r, int x) {
//        int lOff, rOff;
//        lOff = x / 2;
//        rOff = x - lOff;
//        r.left -= lOff;
//        r.right += rOff;
//    }
//
//    private final static int MIN_SHOW_WIDTH = 20;
//    private final static int MIN_SHOW_HEIGHT = 20;
//
//    // not good
//    private void zoom(Point p1, Point p2, Point c1, Point c2) {
//        Rect preRect = makeRect(p1, p2);
//        Rect postRect = makeRect(c1, c2);
//
//        int diffX = postRect.width() - preRect.width();
//        int diffY = postRect.height() - preRect.height();
//
//        mImgRect.left += diffX / 2;
//        mImgRect.right -= diffX - diffX / 2;
//        mImgRect.top += diffY / 2;
//        mImgRect.bottom -= diffY - diffY / 2;
//
//        if (mImgRect.width() > mImgWidth) {
//            mImgRect.left = 0;
//            mImgRect.right = mImgWidth - 1;
//        }
//        if (mImgRect.height() > mImgHeight) {
//            mImgRect.top = 0;
//            mImgRect.bottom = mImgHeight - 1;
//        }
//        if (mImgRect.width() < MIN_SHOW_WIDTH)
//            mImgRect.right = mImgRect.left + MIN_SHOW_WIDTH;
//        if (mImgRect.height() < MIN_SHOW_HEIGHT)
//            mImgRect.bottom = mImgRect.top + MIN_SHOW_HEIGHT;
//
//        if (mImgRect.left < 0)
//            mImgRect.offset(-mImgRect.left, 0);
//        if (mImgRect.top < 0)
//            mImgRect.offset(0, -mImgRect.top);
//        if (mImgRect.right >= mImgWidth)
//            mImgRect.offset(mImgWidth - mImgRect.right, 0);
//        if (mImgRect.bottom >= mImgHeight)
//            mImgRect.offset(0, mImgHeight - mImgRect.bottom);
//    }

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
        // get cavans size
        mCanvasWidth = getWidth() / 2;
        mCanvasHeight = getHeight();

        // size
        mWindowPos = new Point();
        mWindowPos.x = 800;
        mWindowPos.y = 480;

        Resources res = getResources();
        mLeftImage = BitmapFactory.decodeResource(res, mLeftResourceID);
        mRightImage = BitmapFactory.decodeResource(res, mRightResourceID);

        // get image size
        mImgWidth = mLeftImage.getWidth();
        mImgHeight = mRightImage.getHeight();

        mImgRect = new Rect(0, 0, mImgWidth - 1, mImgHeight - 1);
        Bitmap person = GlobalValues.getInstance().getPerson();
        if( person != null ) {
        	mPersonRect = new Rect(0, 0,person.getWidth() - 1, person.getHeight() -1);
        }
        
        if( bMoon == true ) {
        	mMoon = BitmapFactory.decodeResource(res, R.drawable.title_moon);
        	mMoonRect = new Rect(0, 0, mMoon.getWidth() - 1, mMoon.getHeight() -1);
        }

        mCanvasRect = new Rect(0, 0, getWidth(), getHeight());
        mLeftCanvasRect = new Rect(0, 0, mCanvasWidth - 1, mCanvasHeight - 1);
        mRightCanvasRect = new Rect(mCanvasWidth, 0, mCanvasWidth * 2 - 1, mCanvasHeight - 1);

        //this.setClickable(true);
        draw();
		
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
}